import { $http } from "@escook/request-miniprogram";

// 统一封装 HTTP 请求
Object.defineProperty(uni, "$http", {
  value: $http,
  writable: false,
  configurable: false,
});

// 配置请求根路径
$http.baseUrl = "https://api-hmugo-web.itheima.net";

// 请求开始之前做一些事情
$http.beforeRequest = function (options) {
  uni.showLoading({
    title: "数据加载中...",
  });

  // // 如果 store 里有 token，则添加到请求头
  // try {
  //   const store = require("@/store/store.js").default;
  //   if (store?.state?.token) {
  //     options.header = {
  //       ...options.header,
  //       Authorization: `Bearer ${store.state.token}`,
  //     };
  //   }
  // } catch (e) {
  //   console.warn("Store 加载失败:", e);
  // }
};

// 请求完成之后做一些事情
$http.afterRequest = function () {
  uni.hideLoading();
};

// 封装的展示信息提示的方法
uni.$showMsg = function (title = "数据加载失败！", duration = 1500) {
  uni.showToast({
    title,
    duration,
    icon: "none",
  });
};

// #ifndef VUE3
import Vue from "vue";
import App from "./App";
import store from "@/store/store.js";

Vue.config.productionTip = false;
App.mpType = "app";

const app = new Vue({
  ...App,
  store, // Vue2 挂载 store
});

app.$mount();
// #endif

// #ifdef VUE3
import { createSSRApp } from "vue";
import App from "./App.vue";
import store from "@/store/store.js";

export function createApp() {
  const app = createSSRApp(App);
  app.use(store); // Vue3 挂载 store
  return { app };
}
// #endif